<template>
  <div style="height: 100%">
    <el-row style="height: 100%; background: #1aa094">
      <el-col :lg="16" :md="12" class="col-left">
        <h1>Vue3商城管理系统</h1>
        <span>技术支持</span>
      </el-col>
      <el-col :lg="8" :md="12" class="col-right">
        <h1>登录</h1>
        <el-form
          class="login-form"
          ref="ruleFormRefLogin"
          :model="ruleFormLogin"
          :rules="rulesLogin"
        >
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              v-model="ruleFormLogin.username"
            >
              <template #prefix>
                <el-icon>
                  <UserFilled />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" v-model="ruleFormLogin.password">
              <template #prefix>
                <el-icon>
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          class="el-button"
          @click="loginHandle"
          :loading="loading"
          >登录</el-button
        >
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { UserFilled, Lock } from "@element-plus/icons-vue";
import { ref, reactive } from "vue";
import { loginFn, getUserInfoFn } from "@/api/login";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
// 获取仓库实例
import {useStore} from 'vuex'
const store = useStore()
// 获取表单元素
const ruleFormRefLogin = ref(null);
// 定义用户民与密码数据源
const ruleFormLogin = reactive({
  username: "",
  password: "",
});
// 定义用户名与密码验证规则
const rulesLogin = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 5, max: 15, message: "用户名长度应为6-15位", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 5, max: 15, message: "密码长度应为6-15位", trigger: "blur" },
  ],
};
const loading = ref(false);
// 登录
const loginHandle = () => {
  // 校验整个form表单
  ruleFormRefLogin.value.validate(async (isValid) => {
    try {
      if (!isValid) {
        return;
      }
      loading.value = true;
      // 调用登录api
      const res = await loginFn(ruleFormLogin);
      if (!res.data || res.data.status !== 200) {
        return ElMessage.error(res.msg);
      }
      ElMessage({
        message: "登录成功",
        type: "success",
      });
      // token本地存储
      window.sessionStorage.setItem("token", res.data.token);
      router.push("/home");
    } catch (error) {
      console.log(error);
    } finally {
      // 接口调用完成，将loading的值设置为fasle
      loading.value = false;
    }
  });
};
</script>

<style scoped lang="less">
.col-left {
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.col-right {
  background-color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .login-form {
    width: 240px;
  }
}
.el-button {
  width: 240px;
  background-color: #1aa094;
}
</style>
